package czk.concrent.enjoyModelWithNotLock.atomicInterger.practice;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Atomic与i的线程安全对比
 *
 * @Author:ChenZhangKun
 * @Date: 2021/12/4 21:39
 */
public class Compare {
    static int i=1;
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch=new CountDownLatch(1);

        List<Thread> list=new ArrayList<>();

        AtomicInteger ato=new AtomicInteger(1);
        for (int j = 0; j <100 ; j++) {
            Thread t1=new Thread(()->{
                try {
                    latch.await();
                    i++;
                    ato.getAndIncrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t1.start();
            list.add(t1);


        }
        latch.countDown();
        for (Thread thread : list) {
            thread.join();
        }
        System.out.println(ato.get());
        System.out.println(i);
    }
}
